import 'dart:io';

import 'package:bloctest/utils/http/dio_instance.dart';
import 'package:bloctest/utils/loading.dart';
import 'package:flutter/foundation.dart';
import 'package:dio/dio.dart';

import '../../model/home_artcle_model.dart';
import '../../model/home_banner_model.dart';

///获取轮播图
class HomePresent with ChangeNotifier {
  List<HomeItemBanner>? bannerList;
  List<ArticleItems>? homeList;
  int pages = 0;

  Future getBanner() async {
    Response response = await DioInstance.instance().doGet("banner/json", null);
    HomeBannerModel homeBannerModel = HomeBannerModel.fromJson(response.data);
    if (homeBannerModel.bannerList != null) {
      bannerList = homeBannerModel.bannerList;
    } else {
      bannerList = [];
    }
    notifyListeners();
  }

  /// 获取列表
  Future getNotice(isRefresh) async {
    if (isRefresh) {
      pages = 0;
    } else {
      pages++;
    }
    Response response =
        await DioInstance.instance().doGet("article/list/$pages/json", null);
    HomeArticleModel homeArticleModel =
        HomeArticleModel.fromJson(response.data);
    List<ArticleItems>? list = homeArticleModel.datas;
    if (isRefresh) {
      homeList = [];
    }

    if (list?.isNotEmpty == false) {
      pages--;
    }
    homeList?.addAll(list ?? []);
    Loading.dismissLoading();
    notifyListeners();
  }
}
